package com.jh.gmtv.ui.live;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.jh.gmtv.R;
import com.jh.gmtv.bean.ZhiBoBean;
import com.jh.gmtv.ui.home.HomeFragment;
import com.jh.gmtv.util.DownloadJsonDatas;

import java.util.List;

import config.Contasts;

/**
 * A simple {@link Fragment} subclass.
 */
public class LiveFragment extends Fragment {


    private static LiveFragment liveFragment;
    private ImageView mIvThumb;
    private ImageView mIvAvatar;
    private TextView mTvNick;
    private TextView mTvTitle;
    private LiveAdapter adapter;
    private List<ZhiBoBean> zhiBoBean;
    private Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            zhiBoBean = (List<ZhiBoBean>) msg.obj;
            adapter = new LiveAdapter(getContext());
            adapter.setLiveDatas(zhiBoBean);
            mLiveGridView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    };
    private PullToRefreshGridView mLiveGridView;

    public LiveFragment() {
        // Required empty public constructor
    }
    //创建Fragment对象
    public  static Fragment setFragment(){
        if (liveFragment==null)
            synchronized (HomeFragment.class){
                if (liveFragment==null){
                    liveFragment = new LiveFragment();
                }
            }
        return liveFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_live, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        downloadDatas();
        mLiveGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                parent.getItemAtPosition(position);
            }
        });
    }

    private void initView(View view) {
        mIvThumb = (ImageView) view.findViewById(R.id.iv_thumb);
        mIvAvatar = (ImageView) view.findViewById(R.id.iv_avatar);
        mTvNick = (TextView) view.findViewById(R.id.tv_nick);
        mTvTitle = (TextView) view.findViewById(R.id.tv_title);
        mLiveGridView = (PullToRefreshGridView) view.findViewById(R.id.live_gridView);

    }

    private void downloadDatas() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                Message message = Message.obtain();
                zhiBoBean = DownloadJsonDatas.zhiBo(Contasts.live);
                message.what = 1;
                message.obj = zhiBoBean;
                mHandler.sendMessage(message);
            }
        }.start();
    }


}
